package com.dc.base.ui.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.lifecycle.ViewModelProvider;

import com.dc.base.view.LoadingDialog;
import com.dc.base.viewmodel.BaseViewModel;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;

/**
 * @author 冰柠檬dc
 */
public abstract class BaseVmFragment<VM extends BaseViewModel> extends Fragment {

    protected VM mViewModel;

    protected FragmentActivity activity;

    protected LoadingDialog loadingDialog;

    private boolean visibleToUser;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(layoutId(), container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        activity = getActivity();
        mViewModel = createViewModel();
        initView(view);
        initData();
        createObserver();
        initObserve();
    }

    @Override
    public void onResume() {
        super.onResume();
        // 配合ViewPager2实现懒加载
        if (!visibleToUser && !isHidden()) {
            visibleToUser = true;
            lazyLoad();
        }
    }

    /**
     * 没有DataBinding/ViewBinding的布局id
     *
     * @return int
     */
    protected abstract int layoutId();

    /**
     * 初始化视图
     *
     * @param view View
     */
    protected abstract void initView(@NonNull View view);

    /**
     * 初始化数据
     */
    protected abstract void initData();

    /**
     * 创建LiveData数据观察者
     */
    protected void createObserver() {
    }

    /**
     * 懒加载，只有在Fragment第一次创建且第一次对用户客户就加载
     */
    protected void lazyLoad() {

    }

    private void initObserve() {
        mViewModel.getLoadingDialog(this, bean -> {
            if (bean.isShow()) {
                showLoadingDialog(bean.getMsg());
            } else {
                dismissLoadingDialog();
            }
        });
    }

    protected void showLoadingDialog(String msg) {
        if (loadingDialog != null && loadingDialog.isShowing()) {
            loadingDialog.setLoadingMsg(msg);
        } else {
            loadingDialog = new LoadingDialog(activity);
            loadingDialog.setLoadingMsg(msg);
            loadingDialog.show();
        }
    }

    /**
     * 隐藏等待框
     */
    protected void dismissLoadingDialog() {
        if (loadingDialog != null && loadingDialog.isShowing()) {
            loadingDialog.dismiss();
            loadingDialog = null;
        }
    }

    private VM createViewModel() {
        Type type = getClass().getGenericSuperclass();
        if (!(type instanceof ParameterizedType)) {
            return null;
        }
        Type[] arguments = ((ParameterizedType) type).getActualTypeArguments();
        return new ViewModelProvider(this).get((Class<VM>) arguments[0]);
    }
}
